﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Project.Models
{
    public class Utilidades
    {


        //Este metodo es para Proivir el uso de siertos caracteres

        public bool ValidarCadenaString(string[] DatosAValidar) {

            for (int i = 0; i < DatosAValidar.Length; i++) {


                if (ValidarString(DatosAValidar[i])) {

                    return true;
                }
                  }
            return false;
        
        
        }
   
        
        public bool ValidarString(string cadena)
        {
            if (cadena != null)//Si el valor es Null no se hace esto
            {
                cadena = cadena.ToUpper();

                string[] retrincionesCaracteres = { "--", "'", "\\","/*", "*/","/","%", "\"", ";","=", "[" };
                string[] retrincionesComando = { "XP_","SELECT", "DROP", "CREATE", "OR", "ALTER", "RENAME", "REPLACE", "FROM", "INSERT", "UPDATE", "LIKE" };
                char[] delimit = new char[] { ' ' };
                for (int i = 0; i < retrincionesCaracteres.Length; i++)
                {
                    //Convierto el string en mayuscula y Verifico si tiene algunos de esto carateres
                    if (cadena.IndexOf(retrincionesCaracteres[i]) != -1)
                    {

                        return true;
                    }
                }
                // Validar que no tenga uno de los comando Proivido
                foreach (string substr in cadena.Split(delimit))
                {
                    for (int i = 0; i < retrincionesComando.Length; i++)
                    {
                        if (retrincionesComando[i] == substr)
                        {
                            return true;

                        }

                    }
                }
            }
            return false;

        }

       

        //Enviar Email de Validacion al Correro
        public bool enviarEmail(string email, string DefaultPass)
        {

            string From = "Rickycamry07@Hotmail.com";
            string To = email;
            string Message = "Muchas Gracias por preferirnos..  Su Usuario es: " + email + " y su Password es: " + DefaultPass;
            string Subject = "Email de Validacion de Cuenta";
            string smtpServerr = "smtp.live.com"; //"smtp.gmail.com" Este es el Gmail
            System.Net.Mail.MailMessage Email;
            Email = new System.Net.Mail.MailMessage(From, To, Subject, Message);
            System.Net.Mail.SmtpClient smtpMail = new System.Net.Mail.SmtpClient(smtpServerr);
            //Establesco que no es html el cuerpo del email
            try
            {
                Email.IsBodyHtml = false;
                smtpMail.EnableSsl = true;

                smtpMail.UseDefaultCredentials = false;
                //Establesco el puerto del email en este caso el 25
                smtpMail.Port = 25;
                smtpMail.Credentials = new System.Net.NetworkCredential("rickycamry@hotmail.com", "SAGE07");
                //Envio el email
                smtpMail.Send(Email);
                return true;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);

                Console.ReadLine();
                return false;

            }

        }
        public String CrearFolderr(string url)
        {
             
  

            try
            {
                return Directory.CreateDirectory(url).ToString();

            }
            catch (IOException e)
            {

                return e.Message;
            }
        }

        public string PasswordDefault() {

            //Cadena de Posibles Password
            string[] DefaultPassword = { "Ed13014", "Leu13018", "Leu140e8", "Leu130ewr8", "Leu13013er", "ewee301X8", "Ledeu9018", "alq1VEe18", "peyrersyew8", "Le34fgvd343", "vdsjjsuew364", "DDjhyfelde3", "340LEsse8", "Umicro332ut", "bEst97343", "FND434jdfn", "Rdksf4934", "Roek497mM", "F4mmdrKSY45", "Xxeehle93656", "QPxpwx533PP" };
            Random aleatorio = new Random();
            return DefaultPassword[aleatorio.Next(0,(DefaultPassword.Length - 1))];
        }

        //Metodo para crear JSON
        public static string CreateJsonArray(List<string> list)
        {
            if (list.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (string item in list)
                {
                    sb.AppendFormat("'{0}',", item);
                }
                sb.Remove(sb.Length - 1, 1);
                return String.Format("[{0}]", sb.ToString());
            }
            return "[]";
        }

    }
}